package com.hyts.rxjava.bus.context;

import com.hyts.rxjava.bus.event.DefaultEvent;
import com.hyts.rxjava.bus.event.Event;
import com.hyts.rxjava.bus.listener.EventListener;
import com.hyts.rxjava.bus.publish.DefaultEventPublisher;
import com.hyts.rxjava.bus.publish.EventPublisher;
import lombok.Builder;
import rx.Observable;

import java.util.List;
import java.util.Objects;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.rxjava.bus
 * author:Libo/Alex
 * create-date:2019-06-27 17:23
 * email:liboware@gmail.com
 * description:此类主要用于:
 */
@Builder
public class EventBus<T extends Event> {

    /**
     * observable对象
     */
    Observable observable;

    /**
     * 事件发布者
     */
    EventPublisher eventPublisher;

    /**
     * publish注册
     * @return
     */
    protected EventBus event(Object... eventData){
        if(eventPublisher == null) {
            eventPublisher = new DefaultEventPublisher();

        }
        observable = eventPublisher.bind(eventData);
        return this;
    }

    /**
     * publish注册
     * @return
     */
    protected EventBus event(DefaultEvent... eventData){
        if(eventPublisher == null) {
            eventPublisher = new DefaultEventPublisher();
        }
        observable = eventPublisher.publish(eventData);
        return this;
    }

    /**
     * bindListener监听器
     * @param defaultEventEventListener
     * @return
     */
    protected EventBus listener(EventListener<T> defaultEventEventListener){
        observable.subscribe(event->{
            defaultEventEventListener.onEvent((T) event);
        },error->{
            System.err.println("操作错误"+error.toString());
        },()->{
            System.out.println("操作结束");
        });
        return this;
    }

    /**
     * bindListener监听器
     * @param defaultEventEventListener
     * @param errorHandler
     * @return
     */
    protected EventBus listener(EventListener<T> defaultEventEventListener,Observable<T> errorHandler){
        Objects.requireNonNull(errorHandler);
        observable.onErrorResumeNext(errorHandler).subscribe(event->{
            defaultEventEventListener.onEvent((T) event);
            },error->{
                System.err.println("操作错误"+error.toString());
            },()->{
                System.out.println("操作结束");
            });
        return this;
    }

    /**
     * 构建发布
     * @param eventListener
     * @param eventData
     * @return
     */
    @SafeVarargs
    public static EventBus publish(EventListener eventListener, DefaultEvent... eventData){
        return EventBus.builder().build().event(eventData).listener(eventListener);
    }

    /**
     * 构建发布
     * @param eventListener
     * @param eventData
     * @return
     */
    @SafeVarargs
    public static EventBus publish(List<EventListener> eventListener, DefaultEvent... eventData){
        EventBus bus = EventBus.builder().build().event(eventData);
        eventListener.parallelStream().forEach(param->{
            bus.listener(param);
        });
        return bus;
    }

    /**
     * 构建发布
     * @param eventListener
     * @param eventData
     * @return
     */
    @SafeVarargs
    public static EventBus publish(EventListener eventListener,Observable errorHandler,DefaultEvent... eventData){
        return EventBus.builder().build().event(eventData).listener(eventListener,errorHandler);
    }

    /**
     * 构建发布
     * @param eventListener
     * @param eventData
     * @return
     */
    @SafeVarargs
    public static EventBus publish(List<EventListener> eventListener,Observable errorHandler, DefaultEvent... eventData){
        EventBus bus = EventBus.builder().build().event(eventData);
        eventListener.parallelStream().forEach(param->{
            bus.listener(param,errorHandler);
        });
        return bus;
    }
}
